/*
 DNN Content Builder - www.edablio.com/eportal/ContentBuilder
 Copyright (c) 2004-2005
 by Emerson Lopes (elopes@e-dablio.com) of e-Dablio Consultoria e Projetos Ltda

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using epContentBuilder.UserControls.SearchFieldControls;
using System.Collections;

namespace epContentBuilder.UserControls.FieldControls
{
	using System;
	using System.Xml;

	/// <summary>
	///		Summary description for PhotoGallery.
	/// </summary>
	[FormEngineSearchControl(typeof(TextSearchControl))]
	public class PhotoGalleryControl : epContentBuilder.UserControls.FieldControls.BaseFieldControl, IFormlet
	{
		private bool isValid = true;

		private string currentValue = "";

		private System.Collections.Specialized.StringCollection photos = new System.Collections.Specialized.StringCollection();

		private ArrayList list = new ArrayList();

		private XmlDocument xmlDoc = new XmlDocument();
		protected System.Web.UI.WebControls.DataList dlPhotos;
		protected epContentBuilder.UI.WebControls.RadioButtonEx Radiobuttonex1;
		private XmlNode xmlNode;

		private void Page_Load(object sender, System.EventArgs e)
		{
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region IFormlet Members

		public bool IsValid
		{
			get
			{
				return this.isValid;
			}
		}

		public string GetValue(string XML)
		{
			xmlDoc.LoadXml(XML);

			xmlNode = xmlDoc.SelectSingleNode("//SystemType");
			xmlNode.InnerText = "String";

			xmlNode = xmlDoc.SelectSingleNode("//Value");

			foreach (System.Web.UI.Control control in dlPhotos.Controls) 
			{
				foreach (System.Web.UI.Control radio in control.Controls) 
				{
					if (radio is epContentBuilder.UI.WebControls.RadioButtonEx) 
					{
						if (((epContentBuilder.UI.WebControls.RadioButtonEx)radio).Checked) 
						{
							xmlNode.InnerText = ((epContentBuilder.UI.WebControls.RadioButtonEx)radio).RadioValue; 
						}
					}
				}
			}

			if (xmlNode.InnerText == "") 
			{
				isValid = false;
			}

			return xmlDoc.InnerXml;
		}

		public void SetValue(string XML)
		{
			xmlDoc.LoadXml(XML);

			xmlNode = xmlDoc.SelectSingleNode("//Value");

			currentValue = xmlNode.InnerText;

			foreach (System.Web.UI.Control radio in dlPhotos.Controls) 
			{
				if (radio is epContentBuilder.UI.WebControls.RadioButtonEx) 
				{
					if (((epContentBuilder.UI.WebControls.RadioButtonEx)radio).RadioValue  == xmlNode.InnerText) 
					{
						((epContentBuilder.UI.WebControls.RadioButtonEx)radio).Checked = true;
					}
				}
			}
		}

		public void SetConfig(string XML) 
		{
 			xmlDoc.LoadXml(XML);

			xmlNode = xmlDoc.SelectSingleNode("//SourceFolder");

			photos.AddRange(System.IO.Directory.GetFiles(xmlNode.InnerText, "*.jpg"));
			photos.AddRange(System.IO.Directory.GetFiles(xmlNode.InnerText, "*.gif"));

			for (int index = 0;index < photos.Count - 1;index++) 
			{
				photos[index] = photos[index].Replace("\\","/").Substring(photos[index].IndexOf("Portals"));
				list.Add(new Pair(photos[index],""));
			}

			dlPhotos.DataSource = list;
			dlPhotos.DataBind();

		}
		#endregion
	}
}
